#include "game.h" //自定义头文件包含的时候要用 ""

//初始化棋盘
void InitBoard(char board[ROW][COL], int row, int col){
    int i = 0;
    int j = 0;
    for (i=0; i<row; i++){
        for(j=0; j<col; j++){
            board[i][j] = ' ';
        }
    }
}

//打印棋盘
//可变棋盘
void DisplayBoard(char board[ROW][COL], int row, int col){ 
    int i = 0;
    int j = 0;
    //printf("-------------\n");
    for (i=0; i<row; i++){
        for(j=0; j<col; j++){
            printf(" %c ",board[i][j]);
            if (j < col-1)
                printf("|");
        }
        printf("\n");
        if (i < row-1){
            int k = 0;
            for (k=0; k<col; k++){
                printf("---");
                if (k < col-1)
                    printf("|");
            }
            printf("\n");
        }
    }
}

//玩家下棋
void PlayerMove(char board[ROW][COL], int row, int col){
    int x = 0;
    int y = 0;
    printf("玩家下棋:>\n");
    while(1){
        printf("请输入坐标:>");
        //玩家输入坐标应该在[1:3]范围内
        scanf("%d %d", &x, &y); 
        if (x>=1 && x<=row && y>=1 && y<=col) {
            if(board[x-1][y-1] == ' '){
                board[x-1][y-1] = '*';
                break;
            }
            else{
                printf("坐标已占用\n");
            }
        }
        else{
            printf("坐标不合法\n");
        }
    }    
}

//电脑下棋
void ComputerMove(char board[ROW][COL], int row, int col){
    printf("电脑下棋:\n");
    int x = 0;
    int y = 0;
    while(1){
        //生成坐标范围[0:2]
        x = rand() % row; 
        y = rand() % col;
        if(board[x][y] == ' '){
            board[x][y] = '#';
            break;
        }
        else{
            printf("已占用\n");
        }
    }
}

int isFull(char board[ROW][COL],int row, int col){
    int i = 0;
    int j = 0;
    
    for(i=0; i<row; i++){
        for(j=0; j<col; j++){
            if(board[i][j] == ' '){
                return 0;
            }
        }
    }
    return 1;
}

//判断是否赢
char IsWin(char board[ROW][COL],int row, int col){
    //判断行
    int i = 0;
    for (i=0; i<row; i++){
        char flag = board[i][0];//flag为一行的首个棋子
        if(flag == ' '){
            continue;//flag为''，进入下一行判断
        }
        int tag = 1;
        int j = 1;
        for (j=1; j<col; j++){
            //flag不为''，但行中出现与flag不相同的元素，修改tag=0，并退出此行循环
            if (board[i][j] != flag){
                tag = 0;
                break;
            }
        }
        if(tag == 1){
            //循环结束
            //若tag==1，表明此行棋子全部不为空且相同，返回该棋子
            return flag;
        }    
    }

    //判断列
    int j = 0;
    for (j=0; j<col; j++){
        char flag = board[0][j];
        if (flag == ' '){
            continue;
        }
        int tag = 1;
        for (i=0; i<row; i++){
            if (board[i][j] != flag){
                tag = 0;
                break;
            }
        }
        if(tag == 1){
            return flag;
        }
    }
    //判断对角线
    char flag = board[0][0];
    if (flag != ' '){
        int tag = 1;
        for(i=1; i<row; i++){
            if(board[i][i] != flag){
                tag = 0;
                break;
            }
        }
        if(tag == 1){
            return flag;
        }
    }
    //判断反对角线
    flag = board[row-1][0];
    if (flag != ' '){
        int tag = 1;
        for(i=1; i<row; i++){
            if(board[row-i-1][i] != flag){
                tag = 0;
                break;
            }
        }
        if(tag == 1){
            return flag;
        }
    }

    if(isFull(board, row, col)){
        return 'Q';
    }
    return 'C';
}